<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Model {
	function __construct()
	{
		parent::__construct();
		$this->load->database();
	}
	function getUser($phongban)
	{
		$this->db->select('id_nguoidung,chucvu_nguoidung,ten_nguoidung,tenthat_nguoidung');
		$this->db->where('phongban_nguoidung',$phongban);
		$query = $this->db->get('vtv4_nguoidung');
		return $query->result();	
	} 
	function listLich(){
		$this->db->order_by('order_lich DESC');
		$query = $this->db->get('vtv4_danhsachlich');
		return $query->result_array();
	}
	function getTypeTinTuc($id_type_giaoban){
		$this->db->where('id_type_giaoban',$id_type_giaoban);
		$query = $this->db->get('vtv4_type_giaoban');
		return $query->row();
	}
	function getTenLich($id_danhsachlich){
		$this->db->where('id_danhsachlich',$id_danhsachlich);
		$query = $this->db->get('vtv4_danhsachlich');
		return $query->row();
	}
	function listQuyen(){
		$query = $this->db->get('vtv4_quyen');
		return $query->result_array();
	}
	function username($id_nguoidung){
		$this->db->where('id_nguoidung',$id_nguoidung);
		$query = $this->db->get('vtv4_nguoidung');
		return $query->row()->ten_nguoidung;
	}
	function getNgaysinh($ngay)
	{
		$this->db->select('tenthat_nguoidung');
		$this->db->like('ngaysinh_nguoidung',$ngay);
		$query = $this->db->get('vtv4_nguoidung');
		return $query->result();
	}
	function showUser(){
		$this->db->where('id_nguoidung !=',12);
		$this->db->order_by('date_nguoidung',"DESC");
		$query = $this->db->get('vtv4_nguoidung');
		return $query->result();
	}
	
	function getU($id_nguoidung){
		$this->db->distinct();		
		$this->db->where('id_nguoidung',$id_nguoidung);
		$query = $this->db->get('vtv4_nguoidung');
		return $query->row();
	}
	function checkQuyen($id_quyen=null,$type_quyen,$link_quyen=null){
		if($id_quyen != null)
			$this->db->where('id_quyen',$id_quyen);
		$this->db->where('loai_quyen',$type_quyen);
		if($link_quyen != null)
			$this->db->where('link_quyen',$link_quyen);
		$query = $this->db->get('vtv4_quyen');
		if($query -> num_rows() >= 1) return true;
		else return false;
	}
	function getQuyen($id_nguoidung){
		$this->db->where('id_nguoidung',$id_nguoidung);
		$query = $this->db->get('vtv4_nguoidung_quyen');
		return $query->result();
	}
	function username_check($username){
		$this->db->where('ten_nguoidung',$username);
		$this->db->distinct();
		$query = $this->db->get('vtv4_nguoidung');		
		if($query -> num_rows() == 1) return true;
		else return false;
	}
	function password_check($password,$user_id)
	{
		$this->db->where('id_nguoidung', $user_id);
		$this->db->where('matkhau_nguoidung', MD5($password));
		$query = $this->db->get('vtv4_nguoidung');
		if($query -> num_rows() == 1) return true;
		else return false;
	}
	function changePass($data,$user_id)
	{
		$this->db->where('id_nguoidung', $user_id);
		return $this->db->update('vtv4_nguoidung',$data);
	}
	
	function insertUser($data,$quyen)
	{		
		//print_r($data);
		$insert = $this->db->insert('vtv4_nguoidung',$data);
		if(!$insert){
			echo "không insert".$id_nguoidung;	
			return false;
		}
		else{ 
			if(count($quyen) != 0){
				$id_nguoidung = $this->db->insert_id();
				foreach($quyen as $q){
					$fullquyen[] = array(
							'id_nguoidung' => $id_nguoidung,
							'id_quyen' => $q, 
							);
				}
				//return $fullquyen;
				return $this->db->insert_batch("vtv4_nguoidung_quyen",$fullquyen);
			}
			else 
				return true;
		}
	}
	function updateUser($data,$id_nguoidung,$quyen)
	{
		$this->db->where('id_nguoidung',$id_nguoidung);
		$update = $this->db->update('vtv4_nguoidung',$data);
		if(!$update){			
			return false;
		}
		else{								
			$this->db->where('id_nguoidung',$id_nguoidung);
			$ds = $this->db->count_all_results('vtv4_nguoidung_quyen');
			if($ds!=0){						//neu user da co quyen tu truoc
				$this->db->where('id_nguoidung',$id_nguoidung);
				if($this->db->delete('vtv4_nguoidung_quyen')){			//xoa toan bo quyen cu
					if(count($quyen) != 0){
						foreach($quyen as $q){
								$fullquyen[] = array(
										'id_nguoidung' => $id_nguoidung,
										'id_quyen' => $q,
								);
							}							
						return $this->db->insert_batch("vtv4_nguoidung_quyen",$fullquyen);
					}
					else 
						return true;
				}
				else {	
					return false;
				}
			}
			else{	
				if(count($quyen) != 0){
					foreach($quyen as $q){
							$fullquyen[] = array(
									'id_nguoidung' => $id_nguoidung,
									'id_quyen' => $q,
							);
						}								
					return $this->db->insert_batch("vtv4_nguoidung_quyen",$fullquyen);
				}
				else 
					return true;
			}			
		}
	}
	function delUser($id_nguoidung)
	{
		$this->db->where('id_nguoidung',$id_nguoidung);
		return $this->db->delete('vtv4_nguoidung');
	}
}